# Copyright European Organization for Nuclear Research (CERN) since 2012
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import os.path
import sys

base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(base_path)
os.chdir(base_path)

import subprocess  # noqa: E402


def run_git_command(cmd):
    output = subprocess.Popen(["/bin/sh", "-c", cmd], stdout=subprocess.PIPE)
    return output.communicate()[0].strip()


if os.path.isdir('.git'):
    if len(sys.argv) > 1:
        GIT_VERSION = sys.argv[1]
    else:
        GIT_VERSION_CMD = 'git describe --abbrev=4'
        GIT_VERSION = run_git_command(GIT_VERSION_CMD).decode()
    BRANCH_NICK_CMD = r'git branch | grep -Ei "\* (.*)" | cut -f2 -d" "'
    BRANCH_NICK = run_git_command(BRANCH_NICK_CMD).decode()
    REVID_CMD = "git rev-parse HEAD"
    REVID = run_git_command(REVID_CMD).decode()
    REVNO_CMD = "git --no-pager log --oneline | wc -l"
    REVNO = run_git_command(REVNO_CMD).decode()
    VERSION_FILE = open("lib/rucio/vcsversion.py", 'w')
    VERSION_FILE.write("""
'''
This file is automatically generated; Do not edit it. :)
'''
VERSION_INFO = {
    'final': %s,
    'version': '%s',
    'branch_nick': '%s',
    'revision_id': '%s',
    'revno': %s
}
""" % (True, GIT_VERSION, BRANCH_NICK, REVID, REVNO))
    VERSION_FILE.close()

    WEBUI_VERSION_FILE = open("lib/rucio/web/ui/static/webui_version", 'w')
    WEBUI_VERSION_FILE.write('%s' % GIT_VERSION)
    WEBUI_VERSION_FILE.close()
